<template>
  <div>
    <el-form
      ref="loginForm"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="login-box"
    >
      <h3 class="login-title">欢迎登陆</h3>
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          v-model="form.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          native-type="submit"
          @click="onSubmit('loginForm')"
          >登录</el-button
        >
        <el-button native-type="reset">清空</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "admin",
        password: "123"
      },
      //表单验证,需要在 el-form-item 元素中添加 prop 属性
      rules: {
        username: [
          { required: true, message: "账号不可为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不可为空", trigger: "blur" }]
      },
      // 对话框显示和隐藏
      dialogVisible: false
    };
  },
  methods: {
    onSubmit(formName) {
      // 为表单绑定验证功能
      //   var model = this.$refs[formName].model;
      //   console.log(model);
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 使用 vue-router 路由到指定页面
          if (this.form.username == "admin" && this.form.password == "123") {
            //登陆成功
            //增加一个通知
            this.$notify({
              type: "success",
              message: "欢迎你," + this.form.username + "!",
              duration: 5000
            });
            // 带后退的跳转
            this.$router.push("/main/home");
          } else {
            //登录失败
            this.$message({
              type: "error",
              message: "用户名或密码错误",
              showClose: true
            });
          }
        } else {
          // 用户名和密码判断失败
          this.$message.error({
            message: "用户名或密码不能为空",
            showClose: true,
            duration: 10000
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.login-box {
  border: 1px solid #dcdfe6;
  border-radius: 5px; //圆角边框
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399; //盒子阴影
  width: 350px;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
}
.login-title {
  text-align: center;
  margin: 0 auto 40px auto;
  color: #303133;
}
</style>
